package org.fengdi.account.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.fengdi.account.entity.Account;
import org.fengdi.account.mapper.AccountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements AccountService{
    @Autowired
    AccountMapper accountMapper;

    // 扣钱
    public void decreaseMoney(double money, String userId) throws RuntimeException{
        Account req = new Account();
        Account account = this.accountMapper.selectOne(new QueryWrapper<Account>().eq("user_id", userId));
        double currentMoney = account.getMoney();
        double finalMoney = currentMoney - money;
        if (finalMoney < 0) {
            throw new RuntimeException("余额不足");
        }
        req.setMoney(finalMoney);
        req.setUserId(userId);
        this.accountMapper.updateMoneyByUserId(req);
    }

    public Account getAccountByUserId(String userId) {
        return this.accountMapper.getAccountByUserId(userId);
    }
}
